package com.sc.thread.threadcommunication;

/**
 * Created by Shen Chao on 2018-07-03-003.
 * <p>
 * 消费者线程类
 */
public class Consumer extends Thread {
    //共享资源缓存类对象
    private ShareCached sCached;

    /**
     * 构造加入共享资源操作类
     */
    public Consumer(ShareCached sharedCached) {
        this.sCached = sharedCached;
    }

    /**
     * 消费者消费产品，获取共享数据缓存类(仓库)
     * 消费D类型的产品停止消费
     */
    @Override
    public void run() {
        char product = '\u0000';
        do {
            try {
                Thread.sleep((int) (Math.random() * 3000));
            } catch (InterruptedException e) {
                System.out.println("Consumer thread InterruptedException from run method!");
            }
            //消费，从仓库取走商品
            product = sCached.getShareCachedData();
        } while (product != 'D');
    }

}
